package com.study.sms.aliyun.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.QuerySendDetailsRequest;
import com.aliyun.dysmsapi20170525.models.QuerySendDetailsResponse;
import com.aliyun.dysmsapi20170525.models.QuerySendDetailsResponseBody;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.study.constant.RespCodeEnum;
import com.study.sms.aliyun.service.SenderSmsService;
import com.study.sms.aliyun.unitls.DateUtils;
import com.study.sms.aliyun.unitls.SmsClient;
import com.study.sms.entity.vo.BaseRespCode;
import com.study.sms.entity.vo.Result;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Random;

/**
 * @author mjy
 * @date 2021/12/21
 * @description
 */
@Service
public class SenderSmsServiceImpl implements SenderSmsService {


    @Override
    public Result sendSMS(String phone, String template, String templateParams, String signName) {
        Client client = SmsClient.createClient();
        Result result;
        try {
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setSignName(signName)
                    .setPhoneNumbers(phone)
                    .setTemplateParam(templateParams)
                    .setTemplateCode(template);
            String resultCode = client.sendSms(sendSmsRequest).body.code;
            switch (resultCode) {
                case "OK":
                    result = Result.success();
                    break;
                case "isv.MOBILE_NUMBER_ILLEGAL":
                    result = Result.error(RespCodeEnum.SMS_NONSUPPORT_PHONE);
                    break;
                case "isv.BUSINESS_LIMIT_CONTROL":
                    result = Result.error(RespCodeEnum.SMS_PHONE_COUNT_LIMIT);
                    break;
                case "isv.DAY_LIMIT_CONTROL":
                    result = Result.error(RespCodeEnum.SMS_PHONE_DAY_COUNT_LIMIT);
                    break;
                default:
                    result = Result.error(RespCodeEnum.SMS_SEND_ERROR);
            }
        } catch (Exception e) {
            result = Result.error(RespCodeEnum.SMS_SEND_ERROR);
            e.printStackTrace();
        }
        return result;
    }


    @Override
    public Result sendSMSAuthCode(String phone, String template, String signName) {
        String authCode = createAuthCode();
        Result result = sendSMS(phone, template, String.format(TEMPLATE_AUTHCODE_PARAMS, authCode), signName);
        if (BaseRespCode.SUCCESS_CODE == result.getCode()) {
            result.setData(authCode);
        }
        return result;
    }


    @Override
    public Result querySMSLastStatus(String phone) {
        Client client = SmsClient.createClient();
        QuerySendDetailsRequest querySendDetailsRequest = new QuerySendDetailsRequest()
                .setPhoneNumber(phone)
                .setSendDate(DateUtils.format(new Date(), DateUtils.dateFormat11))
                .setCurrentPage(1L)
                .setPageSize(1L);
        String status = "2";//1：等待回执。2：发送失败。3：发送成功。
        try {
            QuerySendDetailsResponse querySendDetailsResponse = client.querySendDetails(querySendDetailsRequest);
            QuerySendDetailsResponseBody body = querySendDetailsResponse.getBody();
            if ("OK".equals(querySendDetailsResponse.getBody().getCode())) {
                QuerySendDetailsResponseBody.QuerySendDetailsResponseBodySmsSendDetailDTOs smsSendDetailDTOs = body.getSmsSendDetailDTOs();
                status = smsSendDetailDTOs.getSmsSendDetailDTO().get(0).getSendStatus() + "";
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return Result.success(status);
    }

    /**
     * 创建6位验证码
     *
     * @return 6位数字
     */
    private static String createAuthCode() {
        return String.format("%06d", new Random().nextInt(999999));
    }



}
